package com.example.commentservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commentservice.entity.Comment;
import com.example.commentservice.entity.vo.CommentVO;
import com.example.commentservice.service.IArticleService;
import com.example.commentservice.service.ICommentService;
import com.example.commentservice.service.IUserService;
import com.example.commentservice.utils.TimeUtils;
import com.example.common.Result;
import jakarta.annotation.Resource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Mr.Tang
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private ICommentService commentService;

    @Resource
    private IUserService userService;

    @Resource
    private IArticleService articleService;


    @PostMapping("/add")
    public Result add(Comment comment,
                      @RequestHeader("uid")Long uid) {
        // 1. 参数校验
        if (comment == null || !StringUtils.hasLength(comment.getContent())
                || comment.getAid() == null || comment.getAid() <= 0
                || uid == null || uid <= 0) {
            return Result.fail(-1, "参数错误");
        }
        // 2. 组装uid
        comment.setUid(uid);
        // 3. 调用service
        boolean result = commentService.save(comment);
        if (!result) {
            return Result.fail(-2, "服务错误");
        }
        // 4. 返回
        return Result.success();
    }


    @PostMapping("/del")
    public Result del(Long cid,
                      @RequestHeader("uid")Long uid) {
        // 1. 参数校验
        if (cid == null || cid <= 0 || uid == null || uid <= 0) {
            return Result.fail(-1, "参数错误");
        }

        // 2. 判断评论归属人
        boolean isOwn = false;
        Comment comment = commentService.getById(cid);
        if (comment == null) {
            return Result.fail(-2, "文章不存在");
        }
        //  2.1 评论是当前登录用户发的
        if (comment.getUid().equals(uid)) {
            isOwn = true;
        }
        //  2.2 评论不是当前登录用户的,但文章是,所有评论均可删除
        if (articleService.getUidByAid(comment.getAid()).equals(uid)) {
            isOwn = true;
        }

        // 3. 调用service
        if (!isOwn) {
            return Result.fail(-3, "无删除权限");
        }
        boolean result = commentService.removeById(cid);
        if (!result) {
            return Result.fail(-1, "服务发生错误");
        }

        // 4. 返回
        return Result.success();
    }

    @GetMapping("/listByAid")
    public Result listByAid(Long aid) {
        // 1. 参数校验
        if (aid == null || aid <= 0) {
            return Result.fail(-1, "无效的文章id");
        }

        // 2. 调用service
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("aid", aid);
        wrapper.orderByDesc("cid");
        List<Comment> list = commentService.list(wrapper);
        List<CommentVO> result = new ArrayList<>();
        // 3. 数据处理 uid -> username
        list.stream().parallel().forEach(comment -> {
            CommentVO commentVO = new CommentVO();
            commentVO.setCid(comment.getCid());
            commentVO.setContent(comment.getContent());
            commentVO.setUid(comment.getUid());
            commentVO.setPid(comment.getPid());
            commentVO.setUsername(userService.getNameByUid(comment.getUid()));
            String time = TimeUtils.toNow(comment.getCreateTime());
            commentVO.setTime(time);
            result.add(commentVO);
        });

        // 4. 返回数据
        return Result.success(result);
    }


    @RequestMapping("/delByAid")
    public boolean delByAid(Long aid) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("aid", aid);
        return commentService.remove(wrapper);
    }

}
